#include "gensess.h"
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <stdlib.h>


typedef unsigned short WORD;

void GenSessionId(char* id)
{
        char reqid[sizeof(time_t) + sizeof(pid_t) + sizeof(short)];
        *((time_t*) reqid) = htonl(time(0));
        *((pid_t*)(reqid + sizeof(time_t))) = htonl(getpid());
        *((WORD*)(reqid + sizeof(time_t) + sizeof(pid_t))) = htons(rand());

        static const char hex[] = "0123456789abcdef";
        
        for (int i=0; i<sizeof(reqid); i++, id+=2)
        {
                id[0] = hex[(reqid[i] >> 4) & 0x0f];
                id[1] = hex[reqid[i] & 0x0f];
        }
        *id = 0;
}
